package com.tsingteng.cosfun.ui.message.geneal;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.PlayCommont;
import com.tsingteng.cosfun.common.Constant;
import com.tsingteng.cosfun.ui.mine.HomeMineFragment;
import com.tsingteng.cosfun.ui.mine.TerminalActivity;
import com.tsingteng.cosfun.utils.ImageUtils;
import com.tsingteng.cosfun.utils.StringUtils;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/7
 * qq(872179966)
 */

public class PlayDetailsAdapter extends BaseQuickAdapter<PlayCommont.CommontList, BaseViewHolder> {
    private OnPlayDetailClickListener onPlayDetailClickListener;

    public PlayDetailsAdapter() {
        super(R.layout.item_details_play_adapter);
    }

    @Override
    protected void convert(BaseViewHolder helper, PlayCommont.CommontList item) {
        CircleImageView headerView = helper.getView(R.id.iv_header);
        TextView mUpName = helper.getView(R.id.tv_name_up);
        TextView mUpComment = helper.getView(R.id.tv_comment_up);
        TextView mAuthor = helper.getView(R.id.tv_author);
        TextView mDownName = helper.getView(R.id.tv_name_down);
        TextView mDownComment = helper.getView(R.id.tv_comment_dowm);
        TextView mTime = helper.getView(R.id.tv_comment_time);
        ImageView mCreat = helper.getView(R.id.iv_great);
        TextView mGreatNum = helper.getView(R.id.tv_great_num);
        ImageView iv_header = helper.getView(R.id.iv_header);
        LinearLayout llCommentGreat = helper.getView(R.id.ll_commeent_great);
        mTime.setText(item.getFormatTime());

        if (!TextUtils.isEmpty(item.getUsetLabel())) {
            mAuthor.setVisibility(View.VISIBLE);
            setText(mAuthor, item.getUsetLabel());
        }else {
            mAuthor.setVisibility(View.GONE);
        }
        String content = StringUtils.matchReplaceTxt(item.getCommentContent());
        SpannableString msp = StringUtils.getWeiBoContent(content, mUpComment,onPlayDetailClickListener);
        if (!TextUtils.isEmpty(item.getReplayContent())) {
            mDownName.setVisibility(View.VISIBLE);
            mDownComment.setVisibility(View.VISIBLE);
            ImageUtils.LoadImage(mContext, item.getReplayUserImage(), headerView);
            setText(mUpName, item.getNickName());
            mUpComment.setText(msp);
//            setText(mUpComment, msp.toString());
            setText(mDownName, item.getReplayUser());
            setText(mDownComment, item.getReplayContent());
        }else {
            ImageUtils.LoadImage(mContext, item.getImage(), headerView);
            mDownName.setVisibility(View.GONE);
            mDownComment.setVisibility(View.GONE);
            setText(mUpName, item.getNickName());
            mUpComment.setText(msp);
        }
        if (item.getPraiseStatus()==0) {
            mCreat.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_small_icon));
        }else {
            mCreat.setImageDrawable(mContext.getResources().getDrawable(R.drawable.great_pink_small_icon));
        }

        llCommentGreat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPlayDetailClickListener !=null) {
                    onPlayDetailClickListener.onPraiseClick(item,mCreat,mGreatNum);
                }
            }
        });
        setText(mGreatNum,String.valueOf(item.getpCount()));

        iv_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int tag = 2;
                int profileId = item.getProfileId();
                Bundle bundle = new Bundle();
                bundle.putInt("user_id",profileId);
                bundle.putInt("tag",tag);
//                bundle.putString(Constant.OTHER,"other");
                bundle.putString("workId",profileId+"");
                bundle.putInt(Constant.TAG, 1);
                TerminalActivity.showFragment(mContext, HomeMineFragment.class, bundle);
            }
        });
    }

    public void setText(TextView textView, String text) {
        if (textView == null || TextUtils.isEmpty(text)) return;
        textView.setText(text);
    }


    public void setOnPlayDetailClickListener(OnPlayDetailClickListener onPlayDetailClickListener) {
        this.onPlayDetailClickListener = onPlayDetailClickListener;
    }
    public interface OnPlayDetailClickListener{
        void onPraiseClick(PlayCommont.CommontList item, ImageView mCreat, TextView mGreatNum);
        void onUserClick(String userId);
    }
}
